package com.Drools.敲定的几种模式;

import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 要支持自定义  (JS 和 Java) 方法
 * JS util(js文件路径,JS的方法,数据)    返回行不行
 * Java util(路径，类名，数据)
 *
 *
 *
 */
public class 决策表 {
    public static void main(String[] args) {
        String realPath = "src/main/resources/rule_xlsx/drools.xlsx";//指定决策表xls文件的磁盘路径
        File file = new File(realPath);
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        SpreadsheetCompiler compiler = new SpreadsheetCompiler();
        String drl = compiler.compile(is, InputType.XLS);
        System.out.println("------------------------------------");
        System.out.println(drl);
        System.out.println("------------------------------------");

        KieHelper kieHelper = new KieHelper();
        kieHelper.addContent(drl, ResourceType.DRL);
//        kieHelper.addContent(drl.replace('1','2'), ResourceType.DRL);
        KieSession kieSession = kieHelper.build().newKieSession();
        Map<String,Object> m = new HashMap<>();
        m.put("1","1");
        m.put("2","2");
        m.put("3","3");
        m.put("4","4");
        kieSession.insert(m);
        int i = kieSession.fireAllRules();
        System.out.println(i);
        kieSession.dispose();
    }
}
